import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../base/Constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../util/BlankToolBarTool.dart';

class FormPage extends StatefulWidget {
  int type;
  FormPage({Key key, this.type}) : super(key: key);

  @override
  _FormPageState createState() => _FormPageState();
}

class _FormPageState extends State<FormPage> {
  String name = "";
  String phone = "";
  String detail = "";
  String token = "";
  BlankToolBarModel barModel = BlankToolBarModel();
  void initState() {
    // TODO: implement initState
    super.initState();
    barModel.outSideCallback = focusNodeChange;
    getString(TOKEN).then((value) {
      token = value;
    });
  }

  void focusNodeChange() {
    setState(() {});
  }

  @override
  void dispose() {
    barModel.removeFocusListeners();
    // AMapLocationClient.shutdown();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: new AppBar(
          title: new Text(
            widget.type == null
                ? "申请合作": "意见反馈",
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black),
          ),
          elevation: 0.5,
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          centerTitle: true,
          iconTheme: new IconThemeData(color: Colors.black),
        ),
        body: BlankToolBarTool.blankToolBarWidget(
          context,
          model: barModel,
          body: new Container(
            child: new Column(
              children: <Widget>[
                initName(),
                new Container(
                  child: Divider(
                    height: 1,
                    color: f0Color,
                  ),
                ),
                initPhone(),
                new Container(
                  child: Divider(
                    height: 1,
                    color: f0Color,
                  ),
                ),
                initDetail(),
                new Container(
                  child: Divider(
                    height: 1,
                    color: f0Color,
                  ),
                ),
                addButton(context)
              ],
            ),
          ),
        ));
  }

  Widget initName() {
    return new Container(
      padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
      color: Colors.white,
      child: new Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Text(
              '联系人:',
              style: TextStyle(
                  fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
            ),
          ),
          new Expanded(
              flex: 3,
              child: new TextField(
                maxLines: 1,
                cursorColor: mainColor,
                onChanged: (value) {
                  name = value;
                },
                keyboardAppearance: Brightness.light,
                controller: TextEditingController.fromValue(TextEditingValue(
                    text: name == null ? "" : name,
                    selection: TextSelection.fromPosition(TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: name == null ? 0 : name.length)))),
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                decoration: new InputDecoration(
                    hintText: "",
                    border: InputBorder.none,
                    hintStyle: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE))),
              )),
        ],
      ),
    );
  }

  Widget initPhone() {
    return new Container(
      padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
      color: Colors.white,
      child: new Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Text(
              '联系方式:',
              style: TextStyle(
                  fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
            ),
          ),
          new Expanded(
              flex: 3,
              child: new TextField(
                maxLines: 1,
                cursorColor: mainColor,
                onChanged: (value) {
                  phone = value;
                },
                controller: TextEditingController.fromValue(TextEditingValue(
                    text: phone,
                    selection: TextSelection.fromPosition(TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: phone.length)))),
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                keyboardAppearance: Brightness.light,
                keyboardType: TextInputType.numberWithOptions(),
                decoration: new InputDecoration(
                    hintText: "",
                    border: InputBorder.none,
                    hintStyle: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE))),
              )),
        ],
      ),
    );
  }

  Widget initDetail() {
    return Container(
      padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
      color: Colors.white,
      child: new Row(
        children: <Widget>[
          // Expanded(
          //   flex: 1,
          //   child: Text(
          //     '详细地址:',
          //     style: TextStyle(
          //         fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
          //   ),
          // ),
          new Expanded(
              // flex: 3,
              child: new TextField(
            maxLines: 4,
            cursorColor: mainColor,
            minLines: 4,
            keyboardAppearance: Brightness.light,
            controller: TextEditingController.fromValue(TextEditingValue(
                text: detail,
                selection: TextSelection.fromPosition(TextPosition(
                    affinity: TextAffinity.downstream,
                    offset: detail.length)))),
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
            onChanged: (value) {
              detail = value;
            },
            decoration: new InputDecoration(
                hintText: "请描述申请原因",
                border: InputBorder.none,
                hintStyle: TextStyle(
                    fontSize:
                        ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE))),
          )),
        ],
      ),
    );
  }

  Widget addButton(BuildContext context) {
    return new Container(
      width: ScreenUtil.getInstance().setWidth(550.00),
      height: ScreenUtil.getInstance().setHeight(DP_80),
      margin: EdgeInsets.fromLTRB(0, 30, 0, 0),
      decoration: new BoxDecoration(
          color: mainColor, borderRadius: BorderRadius.circular(20.0)),
      padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
      child: new FlatButton(
        child: new Padding(
          padding: EdgeInsets.all(0.0),
          child: new Text(
            "提交",
            style: new TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
          ),
        ),
        onPressed: () {
          // showDialog(
          //     context: context,
          //     barrierDismissible: false,
          //     builder: (_) {
          //       return new NetLoadingDialog(
          //         loadingText: "保存中...",
          //         bgColor: Color.fromARGB(255, 0, 0, 0),
          //         requestCallBack: null,
          //         outsideDismiss: false,
          //       );
          //     });
          submit();
        },
      ),
    );
  }

  submit() async {
//     用户意见反馈ID=1  type=3
// 区域代理申请ID=3    type=1
// 推广员申请ID=4    type=2
// 第三方合作方申请ID=5  type=null
    Map map = new Map();
    if (widget.type == 1) {
      map['cat_id'] = 1;
    } else {
      map['cat_id'] = 2;
    }
    map['token'] = token;
    map['content'] = detail;
    map['linkman'] = name;
    map['phone'] = phone;
    FormData data = FormData.fromMap(new Map<String, dynamic>.from(map));
    Response<String> response =
        await HttpUtil().post(Api.URL_ONLINE, data: data);
    print(response);
    Map userMap = json.decode(response.data);
    print(userMap);
    int code = userMap["code"];
    String msg = userMap["msg"];
    // Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    print(userMap);
    // print(userMap['data'].toString());
    if (code == 1) {
      Navigator.of(context).pop("1");
    }
  }
}
